package com.lcy.reggie.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lcy.reggie.common.Result;
import com.lcy.reggie.dto.SetmealDto;
import com.lcy.reggie.entity.Category;
import com.lcy.reggie.entity.Setmeal;
import com.lcy.reggie.entity.SetmealDish;
import com.lcy.reggie.service.CategoryService;
import com.lcy.reggie.service.SetmealDishService;
import com.lcy.reggie.service.SetmealService;
import com.lcy.reggie.mapper.SetmealMapper;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
* @author ChunBaBa_
* @description 针对表【setmeal(套餐)】的数据库操作Service实现
* @createDate 2023-01-17 14:00:28
*/
@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal>
    implements SetmealService{

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private SetmealDishService setmealDishService;

    /**
     *  添加套餐
     * @param setmealDto
     * @return
     */
    @Transactional
    @Override
    public Result<String> saveSetmeal(SetmealDto setmealDto) {
        //1.先保存套餐的基本信息
        this.save(setmealDto);
        //2.保存套餐和菜品的关联信息
        //2.1先用name查询到setmeal表中的数据，获取id
        Setmeal setmeal = this.query().eq("name", setmealDto.getName()).one();
        Long setmealId = setmeal.getId();
        //2.2 获取setmealDto中的setmealDishes集合
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            //因为前端传来的setmealDishes中没有setmeal_id字段，所以逐个添加
            setmealDish.setSetmealId(setmealId.toString());
        }
        //3.将添加好setmeal_id值的setmealDishs导入表中
        setmealDishService.saveBatch(setmealDishes);
        return Result.success("添加成功！");
    }


    /**
     *  分页查询
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public Result<Page> pageResult(Integer page, Integer pageSize,String name) {
        //1.构造分页构造器
        Page<Setmeal> setmealPage = new Page<Setmeal>(page,pageSize);
        //2.创建一个空的 SetmealDto泛型的 构造器
        Page<SetmealDto> setmealDtoPage = new Page<>();
        //2.1 判断有没有name查询
        query().like(StringUtils.isNotBlank(name), "name", name)
                .orderByDesc("update_time").page(setmealPage);
        //3.将setmealPage拷贝给setmealDtoPage，除了records属性
        BeanUtils.copyProperties(setmealPage,setmealDtoPage,"records");
        //4.取出setmealPage中的records集合、
        List<Setmeal> setmealrecords = setmealPage.getRecords();
        List<SetmealDto> list = setmealrecords.stream().map((item) -> {
            //4.1 先创建一个新的SetmealDto
            SetmealDto setmealDto = new SetmealDto();
            //4.2 将item里存储的Setmeal数据，拷贝给setmealDto
            BeanUtils.copyProperties(item, setmealDto);
            //4.3 取出setmealDto内的category_id字段属性
            Long categoryId = setmealDto.getCategoryId();
            //4.4 根据categoryId查询category的name字段，再赋值给categoryName属性
            Category category = categoryService.getById(categoryId);
            setmealDto.setCategoryName(category.getName());

            return setmealDto;
        }).collect(Collectors.toList()); //转为集合

        //5.将list赋值给setmealDtoPage的records
        setmealDtoPage.setRecords(list);
        return Result.success(setmealDtoPage);
    }


    /**
     *  回显功能
     * @param id
     * @return
     */
    @Override
    public Result<SetmealDto> select(Long id) {
        //1.先new一个setmealDto对象
        SetmealDto setmealDto = new SetmealDto();
        //2.将套餐的基本信息拷贝给dto
        Setmeal setmeal = this.getById(id);
        BeanUtils.copyProperties(setmeal,setmealDto);
        //3.通过setmeal_id查询菜品信息
        List<SetmealDish> setmealDishList = setmealDishService.query().eq("setmeal_id", id).list();
        //4.将setmealDishList赋值给dto
        setmealDto.setSetmealDishes(setmealDishList);
        return Result.success(setmealDto);
    }

    /**
     *  修改套餐
     * @param setmealDto
     * @return
     */
    @Transactional
    @Override
    public Result<String> updateSetmeal(SetmealDto setmealDto) {
        //1.先清空setmealDish中之前的数据
        List<SetmealDish> setmealDishes = setmealDishService.query().eq("setmeal_id", setmealDto.getId()).list();
        setmealDishService.removeBatchByIds(setmealDishes);
        //2.再把修改后的数据新增上去
        List<SetmealDish> dishes = setmealDto.getSetmealDishes();
        for (SetmealDish setmealDish : dishes) {
            //因为前端传来的setmealDishes中没有setmeal_id字段，所以逐个添加
            setmealDish.setSetmealId(setmealDto.getId().toString());
        }
        setmealDishService.saveBatch(dishes);

        //3.最后将套餐基础数据修改即可
        this.updateById(setmealDto);
        return Result.success("修改成功！");
    }

    /**
     * 删除和批量删除
     * @param ids
     * @return
     */
    @Transactional
    @Override
    public Result<String> updateByIds(List<Long> ids) {
        //1.先删除setmeal表中的数据
        this.removeBatchByIds(ids);
        //2.再删除setmeal_dish表中的数据
        List<SetmealDish> setmealDishes = setmealDishService.query().in("setmeal_id", ids).list();
        setmealDishService.removeBatchByIds(setmealDishes);
        return Result.success("删除成功！");
    }

    /**
     * 批量修改售卖状态
     * @param status
     * @param ids
     * @return
     */
    @Override
    public Result<String> updateStatus(Integer status, List<Long> ids) {
        List<Setmeal> setmeals = this.query().in("id", ids).list();
        for (Setmeal setmeal : setmeals) {
            setmeal.setStatus(status);
        }
        updateBatchById(setmeals);
        return Result.success("修改成功！");
    }

    /**
     * 手机端回显
     * @param setmeal
     * @return
     */
    @Override
    public Result<List<Setmeal>> listSetmeal(Setmeal setmeal) {
        List<Setmeal> setmealList = this.query().eq("category_id", setmeal.getCategoryId())
                .eq("status", 1)
                .orderByDesc("update_time")
                .list();
        return Result.success(setmealList);
    }
}




